package controllers
/*********************************************************/
/****************本文档中的程序用于终端设备控制相关************/
/*********************version:1.0*************************/
/****Copyright (c) 2014年 许剑华. All rights reserved******/
/*********************************************************/
import (
	//"encoding/json"
	"github.com/astaxie/beego"
	"fmt"
	//"github.com/astaxie/beego/validation"
	//"log"
	//"strconv"
	m "smart_home/models"
	."smart_home/lib"
	//"github.com/gorilla/websocket"
	//"github.com/beego/samples/WebIM/models"
)
type DeviceController struct {
	UserGroup//beego.Controller
}

//设置开关的状态
func (this *DeviceController) SetSwitch() {
	var data P_SET_SWITCH
	var	resp_data ResponeType
	if err := this.ParseForm(&data); err != nil {
		fmt.Println("err",err)
		resp_data = ResponeType{PARA_ERROR,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
	fmt.Println("设置开关",data)
//	var session UserSessionType
//	if data,ok := this.CheckSession(strconv.FormatInt(data.UserID,10),"",&session);ok == false{
//		fmt.Println("用户没有登入")
//		this.Data["json"] = &data
//		this.ServeJson();
//		return
//	}

	user,err:=m.GetUserById(data.UserID)
	if err != nil{
		fmt.Println("err",err)
		//beego.Info("user is nit exit",data.UserID,"IP",this.Ctx.Input.IP())
		resp_data = ResponeType{INVALID_NAME,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
	device := m.DeviceConLog{Mac:data.Mac,OpType:data.OpType,Model:SET_REMOTE_STATUS,DeviceType:data.DeviceType}
	m.AddDeviceLog(&device,&user)
	AddAutoContrSw(data)
	if(!isDeviceExist(data.Mac)){
		beego.Info("device is not exit",data.Mac,"IP",this.Ctx.Input.IP())
		resp_data = ResponeType{OFFLONE,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}else{
		device := m.DeviceConLog{Mac:data.Mac,OpType:data.OpType,Model:SET_DEVICE_STATUS,DeviceType:data.DeviceType}
		m.AddDeviceLog(&device,&user)
		//SendCmd(data.Mac,REQ_RESP_Type{data.Mac,data.DeviceType,INVALID,INVALID,data.OpType,SET_DEVICE_STATUS,INVALID,data.Mask})
		SendCmd(data.Mac,REQ_RESP_Type{RespCode:INVALID,Mac:data.Mac,Model:SET_DEVICE_STATUS,DeviceType:data.DeviceType,IP:INVALIDS,Port:INVALID,ProductName:INVALIDS,TimeScale:INVALID,IRFRQ:0,OpType:data.OpType,Mask:data.Mask})
		resp_data = ResponeType{SUCCESS,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
}
//检查开关的状态
func (this *DeviceController) CheckSwitch() {
	var data P_SET_SWITCH
	var	resp_data ResponeType
	if err := this.ParseForm(&data); err != nil {
		resp_data = ResponeType{PARA_ERROR,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
//	valid := validation.Validation{}
//	b, _ := valid.Valid(&data)
//	if !b {
//		resp_data = ResponeType{PARA_ERROR,INVALIDS,INVALID}
//		this.Data["json"] = &resp_data
//		this.ServeJson();
//		return
//	}
	//var session UserSessionType
	//this.CheckSession(strconv.FormatInt(data.UserID,10),"",&session)
	fmt.Println("检查开关",data)
	if(!isDeviceExist(data.Mac)){
		fmt.Println("device is not exit",data.Mac,"IP",this.Ctx.Input.IP())
		resp_data = ResponeType{OFFLONE,INVALIDS,INVALID}
		this.Data["json"] = &resp_data

		this.ServeJson();
		return
	}else{

		//SendCmd(data.Mac,REQ_RESP_Type{data.Mac,data.DeviceType,INVALID,INVALID,data.OpType,SET_DEVICE_STATUS,INVALID,data.Mask})
//		SendCmd(data.Mac,REQ_RESP_Type{RespCode:INVALID,Mac:data.Mac,Model:SET_DEVICE_STATUS,DeviceType:data.DeviceType,IP:INVALIDS,Port:INVALID,ProductName:INVALIDS,TimeScale:INVALID,IRFRQ:INVALID,OpType:data.OpType,Mask:data.Mask})
//
//		fmt.Println("wait SendCmd return")
		Mask := GetSwitchStatus(data.Mac)
		fmt.Println("检查开关 Mask",Mask)
		resp_data = ResponeType{SUCCESS,data.Mac,Mask}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
}

//设置红外设备
func (this *DeviceController) SetRemote() {

	var remtype SetRemoteType
	var resp_data ResponeType
	if err := this.ParseForm(&remtype); err != nil {
		resp_data = ResponeType{PARA_ERROR,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
//	valid := validation.Validation{}
//	b, _ := valid.Valid(&remtype)
//	if !b {
//		resp_data = ResponeType{PARA_ERROR,INVALIDS,INVALID}
//		this.Data["json"] = &resp_data
//		this.ServeJson();
//		return
//	}
//	var session UserSessionType
//	if data,ok := this.CheckSession(strconv.FormatInt(remtype.UserID,10),"",&session);ok == false{
//		this.Data["json"] = &data
//		this.ServeJson();
//		return
//	}
	user,err:=m.GetUserById(remtype.UserID)    //获取用户
	if err != nil{								//获取用户失败
		beego.Info("user is nit exit",remtype.UserID,"IP",this.Ctx.Input.IP())
		resp_data = ResponeType{INVALID_NAME,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
	device := m.DeviceConLog{Mac:remtype.Mac,OpType:remtype.OpType,Model:SET_REMOTE_STATUS,DeviceType:remtype.DeviceType}
	m.AddDeviceLog(&device,&user)
	AddAutoContrRe(remtype)
	if(!isDeviceExist(remtype.Mac)){
		//fmt.Println("device:",devices[remtype.Mac])
		beego.Info("device is not exit",remtype.Mac,"IP",this.Ctx.Input.IP())
		resp_data = ResponeType{OFFLONE,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}else{
		//SendCmd(remtype.Mac,REQ_RESP_Type{remtype.Mac,remtype.DeviceType,remtype.TimeScale,remtype.IRFRQ,remtype.OpType,SET_REMOTE_STATUS,INVALID,INVALID})
		SendCmd(remtype.Mac,REQ_RESP_Type{RespCode:INVALID,Mac:remtype.Mac,Model:SET_REMOTE_STATUS,DeviceType:remtype.DeviceType,IP:INVALIDS,Port:INVALID,ProductName:INVALIDS,TimeScale:remtype.TimeScale,IRFRQ:remtype.IRFRQ,OpType:remtype.OpType,Mask:remtype.Mask})

		fmt.Println("wait SendCmd return")
		resp_data = ResponeType{SUCCESS,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
}



//是否开启设备控制
func (this *DeviceController)IsOpenMashineLearn(){
	var data AutoControlType
	var	resp_data ResponeType
	if err := this.ParseForm(&data); err != nil {
		resp_data = ResponeType{PARA_ERROR,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
	}
	//data,count:= m.GetParticleSize()
	//data :=  Reg_Log_Type{INVALID_PW,INVALID,INVALID}
	err := m.SetMashineLearn(data.Mac,data.IsOpen)
	if err != nil{
		resp_data = ResponeType{OPERATION_ERROR,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
	}
	resp_data = ResponeType{SUCCESS,INVALIDS,INVALID}
	this.Data["json"] = &resp_data
	this.ServeJson();
}
//获取红外设备的温度及湿度
func (this *DeviceController)GetTemperatureAndHumidity(){
	var data TemperatureType
	var	resp_data ResponeType
	if err := this.ParseForm(&data); err != nil {
		resp_data = ResponeType{PARA_ERROR,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
	}
	if(!isDeviceExist(data.Mac)){
		//fmt.Println("device:",devices[remtype.Mac])
		beego.Info("device is not exit",data.Mac,"IP",this.Ctx.Input.IP())
		resp_data = ResponeType{OFFLONE,INVALIDS,INVALID}
		this.Data["json"] = &resp_data
		this.ServeJson();
	}else {
		//data,count:= m.GetParticleSize()
		//data :=  Reg_Log_Type{INVALID_PW,INVALID,INVALID}
		tem, ok := Get_Temperature(data.Mac)
		if !ok {
			resp_data = ResponeType{OFFLONE, INVALIDS, INVALID}
			this.Data["json"] = &resp_data
			this.ServeJson();
		}
		resp_data = ResponeType{SUCCESS, INVALIDS, tem}
		fmt.Println("获取温度及湿度",resp_data)
		this.Data["json"] = &resp_data
		this.ServeJson();
	}
}


